package com.example.dragon.xmf;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.example.dragon.bean.UserBean;
import com.example.dragon.common.Common;
import com.example.dragon.dialog.MessageDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class UserInfoActivity extends AppCompatActivity {

    private EditText mnaemEdit,mtelEdit;
    String mname,mtel,mavter,imgurl;
    LinearLayout mbackLinearl;
    private SharedPreferences sp;
    private String file="http://xmf.api.0539cn.com/Xmf/Uploads/http://xmf.api.0539cn.com/Xmf/Uploads/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        getUserInfo();
        initViews();
    }
    private void initViews(){
        mnaemEdit = (EditText)findViewById(R.id.usernameEdit);
        mtelEdit = (EditText)findViewById(R.id.userTelEdit);
        mbackLinearl = (LinearLayout)findViewById(R.id.backLayout);
//        sp = getSharedPreferences("userInfo", 0);
//        imgurl = sp.getString("avter",null);
        mbackLinearl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserInfoActivity.this.finish();
            }
        });
    }

    public void updateInfo(View view){
        updateInfo();
    }

    private void updateInfo(){
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"User&a=modInfo")
                .addParams("token",MainActivity.TOKEN)
                .addParams("name",mnaemEdit.getText().toString())
                .addParams("phone",mtelEdit.getText().toString())
                .addParams("idcard","1")
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200){
                                String msg = jsonObject.getString("message");
                                MessageDialog(msg);
                            }else {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
//    http:\/\/xmf.api.0539cn.com\/Xmf\/Uploads\ /20170627\/59513db08fb0e.jpg
//     123456789123456799123456789123456789098765

    private void getUserInfo(){
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"User&a=getInfo")
                .addParams("token",MainActivity.TOKEN)
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200){
                                JSONObject jsonObject1 = jsonObject.getJSONObject("datas");
                                mname = jsonObject1.getString("name");
                                mtel = jsonObject1.getString("mobile");
                                mavter = jsonObject1.getString("avatar");
                                if ("".equals(mavter)){
                                    imgurl=mavter="http:\\/\\/xmf.api.0539cn.com\\/Xmf\\/Uploads\\";
                                    Log.i("maver",mavter);
                                }else {
                                    imgurl=mavter;
                                    Log.i("maver",imgurl);
                                }
                                mnaemEdit.setText(mname);
                                mtelEdit.setText(mtel);
                            }else {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    public void MessageDialog(final CharSequence message){
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                Intent intent = new Intent(UserInfoActivity.this, MainActivity.class);
                intent.putExtra("username",mnaemEdit.getText().toString());
                setResult(10,intent);
                UserInfoActivity.this.finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }
}
